/**
 * 
 */
package com.strat.searcher.analisys.recognize;

import java.util.LinkedList;
import java.util.List;

import com.strat.searcher.analisys.tokenize.Token;

/**
 * Implementation of the Chain of Responsibility pattern for token type
 * recognition.
 * 
 * @author Oleksandr Stratiyevskyy
 * @version $Revision$
 */
public class RecognizionChain implements Recognizable {

    /**
     * chain of Recognizable implementations
     */
    private List<Recognizable> recognizers;

    public RecognizionChain(List<Recognizable> recognizers) {
        this.recognizers = recognizers;
    }

    /**
     * Initialization of the chain
     * 
     * @see com.strat.searcher.analisys.recognize.Recognizable#init()
     */
    public void init() {
        for (Recognizable recognizer : recognizers) {
            recognizer.init();
        }
    }

    /**
     * Applies a chain of Recognizable implementations to the input Token List
     * and transforms it into new Token List.
     * 
     * @param a
     *            List of Tokens.
     * @return new List of Tokens.
     */
    public List<Token> recognize(final List<Token> tokens) {
        List<Token> recognizedTokens = new LinkedList<Token>();
        recognizedTokens.addAll(tokens);
        for (Recognizable recognizer : recognizers) {
            recognizedTokens = recognizer.recognize(recognizedTokens);
        }
        return recognizedTokens;
    }
}